package com.baozhun.mall.common.provider

import androidx.databinding.DataBindingUtil
import com.alibaba.android.arouter.launcher.ARouter
import com.baozhun.mall.common.R
import com.baozhun.mall.common.arouter.RoutePath
import com.baozhun.mall.common.bindadapter.CustomBindAdapter
import com.baozhun.mall.common.databinding.ItemHomeSpecBinding
import com.baozhun.mall.common.model.bean.BrandInfoBean
import com.baozhun.mall.common.model.bean.GoodsInfoBean
import com.baozhun.mall.common.model.bean.SpecInfo
import com.baozhun.mall.common.model.enums.HomeCategoryItemType
import com.baozhun.mall.common.util.Constants
import com.baozhun.mall.common.widget.NumIndicator
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.listener.OnBannerListener

/**
 * @Title：首页专区Provider
 * @Description：
 * @Author：xing.luo
 * @Since：2022/11/09
 * @Version：1.0.0
 */
class HomeSpecProvider : BaseItemProvider<GoodsInfoBean>() {

    override val itemViewType: Int
        get() = HomeCategoryItemType.SPEC.type

    override val layoutId: Int
        get() = R.layout.item_home_spec

    override fun convert(helper: BaseViewHolder, item: GoodsInfoBean) {
        val dataBinding: ItemHomeSpecBinding? = DataBindingUtil.bind(helper.itemView)
        dataBinding?.model = item

        var width = (ScreenUtils.getScreenWidth() - (SizeUtils.dp2px(7f * 3))) / 2
        dataBinding?.bannerSpec?.layoutParams?.width = width
        dataBinding?.bannerSpec?.layoutParams?.height = (width / item.zoneRatio()).toInt()
        dataBinding?.bannerSpec?.setAdapter(object : BannerImageAdapter<SpecInfo?>(item.detail as List<SpecInfo?>?) {
            override fun onBindView(holder: BannerImageHolder, data: SpecInfo?, position: Int, size: Int) {
                CustomBindAdapter.loadImageUrl(holder.imageView, data?.cover)
            }
        })?.setIndicator(NumIndicator(context))?.start()

        dataBinding?.bannerSpec?.setOnBannerListener(object : OnBannerListener<SpecInfo> {
            override fun OnBannerClick(data: SpecInfo?, position: Int) {
                if (data?.zone_type_detail == 1) {
                    ARouter.getInstance().build(RoutePath.Home.ZONE_GOODS_LIST)
                        .withString(Constants.Common.BUNDLE_NAME, data?.zone_id)
                        .navigation()
                } else {
                    ARouter.getInstance().build(RoutePath.Home.BRAND_GOODS_LIST)
                        .withSerializable(
                            Constants.Common.BUNDLE_NAME,
                            BrandInfoBean(data?.zone_id.toString(), data?.channel_name.toString(), data?.cover.toString(), data?.cover.toString(), 0)
                        )
                        .navigation()
                }
            }
        })
    }
}